#include "control.h"
#include "IrControl.h"
#include "Utility.h"

#ifndef _LOOP_PAGES
#define _LOOP_PAGES
#endif

static int focus_up(int focus, int row, int column, int count);
static int focus_down(int focus, int row, int column, int count);
static int focus_left(int focus, int row, int column, int count);
static int focus_right(int focus, int row, int column, int count);
static int focus_page_next(int focus, int row, int column, int count);
static int focus_page_prev(int focus, int row, int column, int count);

int focus_control( int code, int focus, int row, int column, int cnt )
{
	int res = focus;
	switch( code )
	{
	case IRC_UP:
		res = focus_up(focus, row, column, cnt);
		break;
	case IRC_DOWN:
		res = focus_down(focus, row, column, cnt);
		break;
	case IRC_LEFT:		
		res = focus_left(focus, row, column, cnt);
		break;
	case IRC_RIGHT:
		res = focus_right(focus, row, column, cnt);
		break;
	case IRC_NEXT:
		res = focus_page_next(focus, row, column, cnt);
		break;
	case IRC_PREV:
		res = focus_page_prev(focus, row, column, cnt);
		break;
	}
	return res;
}

int focus_up(int focus, int row, int column, int count)
{	
	if ( count <= 0 )
		return focus;
	
	focus -= column;

	if( focus < 0 )
	{
#ifdef _LOOP_PAGES
		if( column == 1 )
			focus = count - 1;
		else
		{
			focus += column;

			focus = focus % column + ( ( count -  1 ) / column ) * column;

			if ( focus >= count )
				focus -= column;
			if( focus < 0 )
				focus = count - 1;
		}
#else
		focus += column;
			
		if( focus > 0 )			// change to right move
			focus--;
#endif
	}
	
	return focus;
}

int focus_down(int focus, int row, int column, int count)
{
	if ( count <= 0 )
		return focus;

	focus += column;

	if( focus >= count )
	{
#ifdef _LOOP_PAGES
		if( column == 1 )
			focus = 0;
		else
		{

			if( focus / column == ( count - 1) / column )
				focus = count - 1;
			else
				focus = focus % column;
		}
#else
		if( column == 1 )
			focus = count - 1;
		else
		{
			if( focus / column == ( count - 1) / column )
				focus = count - 1;
			else
			{
				focus -= column;

				if( focus < count - 1 )	// change to left move
					focus++;
			}
		}
#endif
	}

	return focus;
}

int focus_left(int focus, int row, int column, int count)
{
	if ( count <= 0 )
		return focus;

#ifdef _LOOP_PAGES
	if( focus == 0 )
		return count - 1;
#endif
	if( column == 1 )
	{
		if( count > row * column )
			focus -= row;
		else
			focus -= 1;
	}
	else
		focus -= 1;

	if( focus < 0 )
		focus = 0;

	return focus;
}

int  focus_right(int focus, int row, int column, int count)
{
	if ( count <= 0 )
		return focus;
	
#ifdef _LOOP_PAGES
	if( focus == count - 1 )
		return 0;
#endif
	if( column == 1 )
	{
		if( count > row * column )
			focus += row;
		else
			focus += 1;
	}
	else
		focus += 1;

	if( focus >= count )
		focus = count - 1;

	return focus;
}


int focus_page_next(int focus, int row, int column, int count)
{
	if ( count <= row * column )
		return focus;

//	focus += row * column;
//	if( focus >= count )
//		focus = 0;
	if( focus / row * column == (count-1) / row * column )
		focus %= row * column;
	else
		focus += row * column;

	if( focus >= count )
		focus = count - 1;

	return focus;
}

int focus_page_prev(int focus, int row, int column, int count)
{
	int page = row * column;

	if( count <= page )
		return focus;

	if( focus < page )
		focus += ( count / page ) * page;
	else
		focus -= page;

	if( focus >= count )
		focus = count - 1;

/*	focus -= page;

	if( focus < 0 )
	{
		int imod = count % page;
		focus = imod == 0 ? count - page: count - imod;
	}*/
	return focus;
}
